<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>09_员工列表练习</title>
</head>
<body>
<input type="text" placeholder="请输入员工姓名" id="i1">
<input type="text" placeholder="请输入员工工资" id="i2">
<input type="text" placeholder="请输入员工岗位" id="i3">
<button onclick="addEmp()">添加员工</button>
<hr>
<table border="1px">
    <caption>员工表</caption>
    <tr>
        <th>姓名</th>
        <th>工资</th>
        <th>岗位</th>
    </tr>
</table>

<script>
    //9.准备一个员工数组,用来保存前端收集到的员工数据
    let arr = [];
    //1.定义添加员工的函数
    function addEmp(){
        //2.获取用户在输入框中输入的内容
        let uName = document.getElementById('i1').value;
        let uSalary = document.getElementById('i2').value;
        let uJob = document.getElementById('i3').value;
        console.log(uName,uSalary,uJob);
        //3.每多一个员工,就要多1个tr与3个td,创建对应的元素:
        let tr = document.createElement('tr');
        let nameTd = document.createElement('td');
        let salaryTd = document.createElement('td');
        let jobTd = document.createElement('td');
        //4.将上方收集到的数据装入单元格中
        nameTd.innerHTML = uName;
        salaryTd.innerHTML = uSalary;
        jobTd.innerHTML = uJob;
        //5.将准备好的td装入tr中
        tr.append(nameTd,salaryTd,jobTd);
        //6.获取表格元素
        let table = document.querySelector('table');
        //7.将准备好的tr装入表格中
        table.append(tr);
        //8.用完的数据可以清空
        document.getElementById('i1').value = '';
        document.getElementById('i2').value = '';
        document.getElementById('i3').value = '';
        //9.将收集到的当前数据存入数组中
        arr.push({
            name:uName,
            salary:uSalary,
            job:uJob
        })
        console.log(arr);
    }
</script>
</body>
</html>